<script setup lang="ts">
import { ref, onMounted, computed } from 'vue'
import axios from 'axios'
// 移除了 useRouter 导入，因为我们不再使用路由跳转

// 定义比赛信息接口
interface Game {
  gamesId: number
  quaId: number
  orId: number
  gameName: string
  createTime: string
  appTime: string | null
  readyTime: string | null
  startTime: string | null
  endTime: string | null
  synopsis: string | null
  attachment: string | null
}

// 定义 emit 事件
const emit = defineEmits(['modify-game', 'create-game', 'manage-game-items'])

// 响应式数据
const games = ref<Game[]>([])
const loading = ref(true)
const error = ref<string | null>(null)
const currentPage = ref(1)
const pageSize = ref(2) // 每页显示2条数据
// 移除了 router 实例，因为我们不再使用路由跳转

// 计算总页数
const totalPages = computed(() => {
  return Math.ceil(games.value.length / pageSize.value)
})

// 计算当前页的数据
const paginatedGames = computed(() => {
  const startIndex = (currentPage.value - 1) * pageSize.value
  const endIndex = startIndex + pageSize.value
  return games.value.slice(startIndex, endIndex)
})

// 获取比赛信息
const fetchGames = async () => {
  try {
    loading.value = true
    const token = localStorage.getItem('token')
    const response = await axios.get('http://localhost:8080/game/getCurrentGame', {
      headers: {
        'token': token || ''
      }
    })
    
    games.value = response.data
    error.value = null
  } catch (err) {
    console.error('获取比赛信息失败:', err)
    error.value = '获取比赛信息失败'
  } finally {
    loading.value = false
  }
}

// 比赛项目管理处理函数
const manageGameItems = (game: Game) => {
  // 发送 manage-game-items 事件，传递整个比赛对象
  emit('manage-game-items', game)
}

// 修改比赛信息处理函数 - 发送事件而不是路由跳转
const modifyGame = (game: Game) => {
  // 发送 modify-game 事件，传递整个比赛对象
  emit('modify-game', game)
}

// 页面切换函数
const changePage = (page: number) => {
  if (page >= 1 && page <= totalPages.value) {
    currentPage.value = page
  }
}

// 新建比赛项目
const createNewGame = () => {
  // 发送 create-game 事件
  emit('create-game')
}

// 组件挂载时获取数据
onMounted(() => {
  fetchGames()
})
</script>

<template>
  <div class="game-management">

    <!-- 加载状态 -->
    <div v-if="loading" class="loading">
      数据加载中...
    </div>
    
    <!-- 错误状态 -->
    <div v-else-if="error" class="error">
      {{ error }}
    </div>
    
    <!-- 数据展示 -->
    <div v-else class="games-container">
      <div v-if="games.length === 0" class="no-data">
        暂无比赛信息
      </div>
      
      <div v-else>
        <div class="games-list">
          <div 
            v-for="game in paginatedGames" 
            :key="game.gamesId" 
            class="game-card"
          >
            <h3>{{ game.gameName }}</h3>
            <div class="game-info">
              <p><strong>比赛ID:</strong> {{ game.gamesId }}</p>
              <p><strong>创建时间:</strong> {{ new Date(game.createTime).toLocaleString() }}</p>
              <div v-if="game.appTime"><strong>报名时间:</strong> {{ new Date(game.appTime).toLocaleString() }}</div>
              <div v-if="game.readyTime"><strong>准备时间:</strong> {{ new Date(game.readyTime).toLocaleString() }}</div>
              <div v-if="game.startTime"><strong>开始时间:</strong> {{ new Date(game.startTime).toLocaleString() }}</div>
              <div v-if="game.endTime"><strong>结束时间:</strong> {{ new Date(game.endTime).toLocaleString() }}</div>
              <div v-if="game.synopsis"><strong>简介:</strong> {{ game.synopsis }}</div>
            </div>
            <div class="game-actions">
              <button class="manage-button" @click="manageGameItems(game)">比赛项目管理</button>
              <button class="modify-button" @click="modifyGame(game)">修改比赛信息</button>
            </div>
          </div>
        </div>
        
        <!-- 分页控件 -->
        <div class="pagination" v-if="totalPages > 1">
          <button 
            :disabled="currentPage === 1" 
            @click="changePage(currentPage - 1)"
            class="page-button"
          >
            上一页
          </button>
          
          <span class="page-info">
            第 {{ currentPage }} 页，共 {{ totalPages }} 页
          </span>
          
          <button 
            :disabled="currentPage === totalPages" 
            @click="changePage(currentPage + 1)"
            class="page-button"
          >
            下一页
          </button>
        </div>
      </div>
    </div>
  </div>
</template>

<style scoped>
.game-management {
  padding: 20px;
}

.game-management h2 {
  color: #333;
  margin-bottom: 20px;
  text-align: center;
}

.header-actions {
  text-align: right;
  margin-bottom: 20px;
}

.create-button {
  background: linear-gradient(to right, #31B310, #2a990e);
  color: white;
  border: none;
  border-radius: 5px;
  padding: 10px 20px;
  font-size: 16px;
  cursor: pointer;
  transition: all 0.3s ease;
}

.create-button:hover {
  transform: translateY(-2px);
  box-shadow: 0 4px 8px rgba(0, 0, 0, 0.2);
}

.loading, .error, .no-data {
  text-align: center;
  padding: 40px 20px;
  font-size: 18px;
}

.error {
  color: #e74c3c;
}

.no-data {
  color: #7f8c8d;
}

.games-list {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  grid-template-rows: repeat(1, 1fr);
  gap: 20px;
  margin-bottom: 30px;
  background: none;
  border: none;
  box-shadow: none;
  padding: 0;
}

.game-card {
  background: white;
  border-radius: 8px;
  padding: 25px;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
  transition: transform 0.3s ease, box-shadow 0.3s ease;
  border-left: 5px solid #31B310;
  min-height: 270px;
  display: flex;
  flex-direction: column;
  justify-content: center;
}

.game-card:hover {
  transform: translateY(-5px);
  box-shadow: 0 5px 20px rgba(0, 0, 0, 0.15);
}

.game-card h3 {
  color: #31B310;
  margin-top: 0;
  border-bottom: 2px solid #eee;
  padding-bottom: 15px;
  text-align: center;
}

.game-info {
  flex-grow: 1;
  display: flex;
  flex-direction: column;
  justify-content: center;
}

.game-info p {
  margin: 10px 0;
  color: #555;
}

.game-info div {
  margin: 8px 0;
  color: #555;
}

.game-actions {
  margin-top: 20px;
  text-align: center;
}

.modify-button {
  background: linear-gradient(to right, #3498db, #2980b9);
  color: white;
  border: none;
  border-radius: 5px;
  padding: 10px 20px;
  font-size: 14px;
  cursor: pointer;
  transition: all 0.3s ease;
}

.modify-button:hover {
  transform: translateY(-2px);
  box-shadow: 0 4px 8px rgba(0, 0, 0, 0.2);
}

.manage-button {
  background: linear-gradient(to right, #9b59b6, #8e44ad);
  color: white;
  border: none;
  border-radius: 5px;
  padding: 10px 20px;
  font-size: 14px;
  cursor: pointer;
  transition: all 0.3s ease;
  margin-right: 15px;
}

.manage-button:hover {
  transform: translateY(-2px);
  box-shadow: 0 4px 8px rgba(0, 0, 0, 0.2);
}

.pagination {
  display: flex;
  justify-content: center;
  align-items: center;
  gap: 20px;
  margin-top: 30px;
}

.page-button {
  background: #3498db;
  color: white;
  border: none;
  border-radius: 5px;
  padding: 8px 16px;
  cursor: pointer;
  transition: all 0.3s ease;
}

.page-button:hover:not(:disabled) {
  background: #2980b9;
  transform: translateY(-2px);
}

.page-button:disabled {
  background: #bdc3c7;
  cursor: not-allowed;
}

.page-info {
  color: #333;
  font-weight: bold;
}
</style>